//
//  DoNotDisturbViewController.m
//  Lightthings
//
//  Created by 王岩 on 2024/11/29.
//

#import "DoNotDisturbViewController.h"

@interface DoNotDisturbViewController ()
@property (weak, nonatomic) IBOutlet UILabel *openLab;
@property (weak, nonatomic) IBOutlet UILabel *startLab;
@property (weak, nonatomic) IBOutlet UILabel *endLab;
@property (weak, nonatomic) IBOutlet UIButton *startBtn;
@property (weak, nonatomic) IBOutlet UIButton *endBtn;
@property (weak, nonatomic) IBOutlet UIButton *closeBtn;
@property (weak, nonatomic) IBOutlet UIButton *openBtn;
@property (weak, nonatomic) IBOutlet UIButton *allDayBtn;
@property (nonatomic, assign) unsigned long switchValue, start_H, start_M, end_H, end_M;

@end

@implementation DoNotDisturbViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.jy_title = SWString(@"勿扰设置");
    self.openLab.text = SWString(@"On/Off");
    self.startLab.text = SWString(@"Timer");
    self.endLab.text = SWString(@"End Time");
    
    [SWInitializer jy_initRedButtonWithTarget:self action:@selector(clickSureBtnAction) frame:CGRectMake(0, 0, kScreenWidth - 100, 50) title:SWString(@"Confirm") on:self.view layout:^(MASConstraintMaker * _Nonnull make) {
        kMakeLV(50);
        kMakeBV(-100);
        kMakeWHV(kScreenWidth-100, 50);
    }];

    [self updateUI];
    [self readData];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)updateUI {
    if (self.openBtn.selected) {
        self.startBtn.hidden = self.startLab.hidden = self.endBtn.hidden = self.endLab.hidden = NO;
        NSString * start = [NSString stringWithFormat:@"%02lu:%02lu",self.start_H, self.start_M];
        NSString * end = [NSString stringWithFormat:@"%02lu:%02lu",self.end_H, self.end_M];
        
        [self.startBtn setTitle:start forState:0];
        [self.endBtn setTitle:end forState:0];
    }else {
        self.startBtn.hidden = self.startLab.hidden = self.endBtn.hidden = self.endLab.hidden = YES;
    }
    
}

- (void)readData {
    [[WatchCommandTool sharedInstance] readStormCommand:Command_DoNotDisturb ToPeripheral:self.peripheral reBack:^(NSString * _Nonnull backData) {
        if (backData) {
            NSString * valueStr = [backData substringWithRange:NSMakeRange(26, 2)];
            unsigned long value = strtoll([[NSString stringWithFormat:@"0x%@",valueStr] UTF8String], 0, 16);
            self.switchValue = value;
            NSLog(@"开关数值：%lu",value);
          
            
            [self updateUI];
            
        }
    }];
}

- (void)clickSureBtnAction {
    
}

- (IBAction)clickSwithBtn:(UIButton *)sender {
    self.closeBtn.selected = sender.tag==100;
    self.openBtn.selected = sender.tag==200;
    self.allDayBtn.selected = sender.tag==300;
    [self updateUI];
    
}
- (IBAction)clickStartBtn:(UIButton *)sender {
    [BRDatePickerView showDatePickerWithMode:BRDatePickerModeHM title:nil selectValue:nil resultBlock:^(NSDate * _Nullable selectDate, NSString * _Nullable selectValue) {
        NSArray * arr = [selectValue componentsSeparatedByString:@":"];
        self.start_H = [arr.firstObject longLongValue];
        self.start_M = [arr.lastObject longLongValue];
        [self updateUI];
    }];
}
- (IBAction)clickEndBtn:(UIButton *)sender {
    [BRDatePickerView showDatePickerWithMode:BRDatePickerModeHM title:nil selectValue:nil resultBlock:^(NSDate * _Nullable selectDate, NSString * _Nullable selectValue) {
        NSArray * arr = [selectValue componentsSeparatedByString:@":"];
        self.end_H = [arr.firstObject longLongValue];
        self.end_M = [arr.lastObject longLongValue];
        [self updateUI];
    }];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
